package com.zoro.barn.business.service.pp.business.project.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.zoro.barn.business.service.pp.business.project.dto.BarnProjectDto;
import com.zoro.barn.business.service.pp.business.project.dto.CloneProjectDto;
import com.zoro.barn.business.service.pp.business.project.entity.BarnProject;
import com.zoro.barn.business.service.pp.business.project.entity.BarnProjectUser;
import com.zoro.barn.business.service.pp.business.project.service.BarnProjectService;
import com.zoro.barn.commons.tools.json.JsonUtil;
import com.zoro.barn.commons.webcommons.tools.WebContentUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhaoxingwu
 */
@RestController
@RequestMapping("/project")
public class BarnProjectController {

    private final BarnProjectService projectService;

    public BarnProjectController(BarnProjectService projectService) {
        this.projectService = projectService;
    }

    @RequestMapping("/query")
    public List<BarnProject> query(BarnProjectUser barnProject) {
        return this.projectService.query(barnProject);
    }

    @RequestMapping("/getById/{pid}")
    public BarnProjectDto getById(@PathVariable String pid) throws JsonProcessingException {


        BarnProject project = this.projectService.getById(pid);
        BarnProjectDto result = JsonUtil.transformType(project, BarnProjectDto.class);
        if (result != null) {
            result.setCountSql(projectService.queryProjectSql(pid));
            result.setCountDocument(projectService.queryProjectDocument(pid));
        }
        return result;
    }

    @RequestMapping("/save")
    public BarnProject save(@RequestBody BarnProject barnProject) {
        String loginId = WebContentUtil.requestUserInfo().getPid();
        if (StringUtils.isNotBlank(barnProject.getPid())) {
            // update
            barnProject.setUpdateId(loginId);
            return this.projectService.update(barnProject);
        } else {
            // insert
            barnProject.setCreateId(loginId);
            return this.projectService.insert(barnProject);
        }
    }

    @RequestMapping("/mine")
    public List<BarnProject> mine(BarnProjectUser param) {
        String pid = WebContentUtil.requestUserInfo().getPid();
        param.setUserId(pid);
        return this.projectService.query(param);
    }

    /**
     * clone one project's sql and product,this will delete sql and product that the project already had
     *
     * @param param param
     */
    @RequestMapping("/cloneProject")
    public void cloneProject(@RequestBody CloneProjectDto param) {
        this.projectService.cloneProject(param.getFromProjectId(), param.getToProjectId());
    }

}
